package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.ReturnListGoodsDao;
import com.atguigu.jxc.service.ReturnListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import com.atguigu.jxc.vo.GoodVo;
import org.springframework.stereotype.Service;
import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.entity.*;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.SecurityUtils;

import java.io.IOException;
import java.util.List;

import java.util.List;
import java.util.Map;

@Service
public class ReturnListGoodsServiceImpl implements ReturnListGoodsService {
    @Autowired
    private ReturnListGoodsDao returnListGoodsDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private GoodsDao goodsDao;

    private static final ObjectMapper MAPPER = new ObjectMapper();


    @Override
    public List<GoodVo> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        return this.returnListGoodsDao.count(sTime, eTime, goodsTypeId, codeOrName);
    }

    @Override
    public void delete(Integer returnListId) {
        returnListGoodsDao.delete(returnListId);
    }

    @Override
    public Map<String, Object> goodsList(Integer returnListId) {
        Map<String, Object> map=returnListGoodsDao.goodsList(returnListId);
        return map;
    }

    @Override
    public Map<String, Object> list(String returnNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        Map<String, Object> map=returnListGoodsDao.list(returnNumber,supplierId,state,sTime,eTime);
        return map;
    }

    @Override
    public void save(ReturnList returnList, String returnListGoodsStr) {

        try {
            // 1. 保存退货清单
            //      设置当前操作用户
            User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
            returnList.setUserId(currentUser.getUserId());
            this.returnListGoodsDao.saveReturnList(returnList);

            // 2. 保存货物更新单，有多个
            //      解析 json
            List<ReturnListGoods> returnListGoodsList = MAPPER.readValue(returnListGoodsStr, new TypeReference<List<ReturnListGoods>>() { });
            returnListGoodsList.forEach(returnListGoods -> {
                // 获取该货物单的货物
                Goods goods = goodsDao.findByGoodsId(returnListGoods.getGoodsId());
                // 设置进货清单编号
                returnListGoods.setReturnListId(returnListGoods.getReturnListId());
                // 设置货物种类编号
                returnListGoods.setGoodsTypeId(goods.getGoodsTypeId());
                // 保存
                this.returnListGoodsDao.saveReturnListGoods(returnListGoods);

                // 3. 更新货存信息
                // 更新库存数量
                goods.setInventoryQuantity(goods.getInventoryQuantity() - returnListGoods.getGoodsNum());
                // 上次进货价格
                goods.setLastPurchasingPrice(returnListGoods.getPrice());
                this.goodsDao.updateGoods(goods);
            });


        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("解析 json 失败");
        }
    }
}
